import { _decorator, Component, find, instantiate, Prefab, resources, Sprite, tween } from 'cc';
import Banner from '../../../Scripts/Banner';
import { UIManager } from '../../../Scripts/Framework/Managers/UIManager';
import { Tools } from 'db://assets/Scripts/Framework/Utils/Tools';
const { ccclass, property } = _decorator;

@ccclass('TreasureBox_Main')
export default class TreasureBox_Main extends Component {
    @property(Sprite)
    jdt: Sprite | null = null;//进度条
    index: number = 0;
    AwardCallBack: Function = null;
    public static ShowTreasureBox(AwardCallBack?: Function) {
        resources.load("Prefabs/UI/TreasureBox", Prefab, (err, date) => {
            if (err) {
                console.log("没有找到宝箱预制体");
                return;
            }
            console.log("生成宝箱");
            let pre = instantiate(date);
            pre.setParent(find("Canvas"));
            pre.setSiblingIndex(99999);
            pre.setPosition(0, 0, 0);
            if (AwardCallBack) {
                pre.getComponent(TreasureBox_Main).AwardCallBack = AwardCallBack;
            }
        });
    }
    gived: boolean = false;
    sussCb: Function = null;
    Show(sussCb: Function = null) {
        this.IsShow2 = false;
        this.IsShow = false;
        this.sussCb = sussCb;
        this.gived = false;
        tween(this.node.getChildByName("其他").getChildByName("光效"))
            .to(2, { angle: 360 })
            .to(0, { angle: 0 })
            .union()
            .repeatForever()
            .start();
        this.schedule(() => {
            if (this.index > 0) this.index -= 1;
        }, 0.1)
    }
    IsShow: boolean = false;
    IsShow2: boolean = false;
    //    //点击宝箱按钮
    OnButtonClick() {
        this.index += Tools.GetRandom(5, 10);
        if (this.index >= 60 && !this.IsShow) {
            Banner.Instance.ShowVideoAd(() => { this.GiveReward(); });
            this.IsShow = true;
            this.scheduleOnce(() => {
                this.sussCb && this.sussCb();
                this.node.destroy();
            }, 1);
        }
    }
    protected update(dt: number): void {
        this.jdt.fillRange = this.index / 100;
    }

    protected lateUpdate(dt: number): void {
        if (this.index > 0) this.index -= 0.1;
    }

    //    //提供奖励且销毁
    GiveReward() {
        if (this.gived) return;
        this.gived = true;
        if (this.AwardCallBack) {
            this.AwardCallBack();
        }
    }
}


/**
 * 注意：已把原脚本注释，由于脚本变动过大，转换的时候可能有遗落，需要自行手动转换
 */
// import Banner from "./Banner";
// import { DataManager } from "./Framework/Managers/DataManager";
// import { UIManager } from "./Framework/Managers/UIManager";
// import { ZTool } from "./Framework/Utils/ZTool";
//
//
// const { ccclass, property } = cc._decorator;
//
// @ccclass
// export default class TreasureBox extends cc.Component {
//     @property(cc.Sprite)
//     jdt: cc.Sprite = null;//进度条
//     index: number = 0;
//
//     public static ShowTreasureBox() {
//         cc.resources.load("Prefabs/宝箱", cc.Prefab, (err, date) => {
//             if (err) {
//                 console.log("没有找到宝箱预制体");
//                 return;
//             }
//             console.log("生成宝箱");
//             let pre = cc.instantiate(date);
//             pre.setParent(cc.find("Canvas"));
//             pre.setSiblingIndex(99999);
//             pre.setPosition(0, 0, 0);
//         });
//
//     }
//
//     gived: boolean = false;
//     successCb: Function = null;
//
//     Show(successCb: Function = null) {
//         this.IsShow2 = false;
//         this.IsShow = false;
//         this.successCb = successCb;
//         this.gived = false;
//         cc.tween(this.node.getChildByName("其他").getChildByName("光效"))
//             .to(2, { angle: 360 })
//             .to(0, { angle: 0 })
//             .union()
//             .repeatForever()
//             .start();
//         this.schedule(() => {
//             if (this.index > 0) this.index -= 1;
//         }, 0.1)
//
//         Banner_Main.Instance.ShowNativeAd();
//     }
//
//     IsShow: boolean = false;
//     IsShow2: boolean = false;
//
//     //点击宝箱按钮
//     OnButtonClick() {
//         this.index += 5;
//         if (this.index >= 10 && !this.IsShow2) {
//             Banner_Main.Instance.ShowNativeAd();
//             this.IsShow2 = true;
//         }
//
//         if (this.index >= 60 && !this.IsShow) {
//             Banner.Instance.ShowVideoAd(() => { this.GiveReward(); });
//             this.IsShow = true;
//             this.scheduleOnce(() => {
//                 this.successCb && this.successCb();
//                 let money = ZTool.GetRandomInt(10000, 600000);
//                 UIManager.ShowTip(`获得金钱${money}`);
//                 DataManager.Money += money;
//                 this.node.destroy();
//             }, 1);
//         }
//     }
//
//     protected update(dt: number): void {
//         this.jdt.fillRange = this.index / 100;
//     }
//
//     //提供奖励且销毁
//     GiveReward() {
//         if (this.gived) return;
//         this.gived = true;
//         let money = ZTool.GetRandomInt(10000, 600000);
//         UIManager.ShowTip(`获得金钱${money}`);
//         DataManager.Money += money;
//     }
// }
